$(function(){
	$('.set_time').hide();
	$('.input').focus(function(){
		$('.error_msg').text('');
//		$(this).select();
	})
//	判断input是否为空
	$('.input').bind('input propertychange',function(){
		if($("#userMobile").val() != "" && $("#temleId").val() != ""){
			$("#button").removeAttr("disabled");
			$("#button").css("background","#FF5000");
		}else{
			$("#button").attr("disabled","disabled");
			$("#button").css("background","#999");
		}
	})
	function telCheck(){
		//	号码格式验证函数
		var userMobile = $('#userMobile').val();
		var pattern=/^1(3|4|5|7|8)\d{9}$/;
		if(userMobile.length<11&&userMobile.length>0){
			$('.error_msg').text('*手机号码必须为11位');
			return false;
		}else if(!pattern.test(userMobile)){
			$('.error_msg').text('*手机号码格式不正确');
			return false;
		}else{	
			return true;
		}
	}
	//	短信验证码获取事件
	$('.get_tId').click(function(){
		if(telCheck()){
			var userMobile = $('#userMobile').val();
			console.log(userMobile);
			var data = {userMobile:userMobile};
			console.log(data);
//			查询手机号是否重复
			$.post(constans.globalUrl+'/user/mobver',data,function(data){
				console.log(data);
				if(data.code == 'FAIL'){				
					getNote();
				}else{
//					$('.error_msg').text(data.message);		
					$('.error_msg').text('手机号不存在，请先注册');	
				}
			})
		}
	})
//		短信获取验证码函数
	var oldcode = {captcha:0,userMobile:0,oldTime:0};
	function getNote(){
		$('.get_tId').hide();
		$('.set_time').show();
		var num = 60;
		$('.set_time em').text(num);
		timer = setInterval(function(){
			num--;
			$('.set_time em').text(num);
		},1000)
		setTimeout(function(){
			$('.get_tId').show();
			$('.set_time').hide();
			clearInterval(timer);
		},60000)
		var userMobile = $('#userMobile').val();
		var oldTime =Date.parse(new Date());
		var data = {userMobile:userMobile,templeId:'2345'};
		console.log(data);
		$.post(constans.globalUrl+'/support/sendcode',data,function(data){
			console.log(data);
			if(data.code == 'SUCCESS'){
				alert('短信发送成功,验证码在15分钟内有效');
				oldcode = {captcha:data.message,userMobile:userMobile,oldTime:oldTime}	;		
			}
		})
	}
//	验证码验证函数
	function nodeCheck(){
		console.log(oldcode);
		var webCaptcha=$('#temleId').val();
		var userMobile = $('#userMobile').val();
		console.log(webCaptcha);
		var newTime = Date.parse(new Date());	
//			if((newTime-oldcode.oldTime)<15*60*1000 && oldcode.oldTime!=0){			
				if(oldcode.userMobile==userMobile && oldcode.captcha == webCaptcha){
					return true;
				}else{
					$('.error_msg').text('验证码错误')	
					return false;
				}
//			}
		}
	
	//	找回密码下一步
	$('.find_button').click(function(){
		if(telCheck()){	
			var userMobile = $('#userMobile').val();
			var data = {userMobile:userMobile};
			$.post(constans.globalUrl+'/user/mobver',data,function(data){
				if(data.status == 'SUCCESS'){				
//					$('.form .error_msg').text(data.message);
					$('.form .error_msg').text('手机号码不存在，请先注册');
					return;
				}else{					
					if(nodeCheck()){
						console.log(userMobile);
						$.cookie('userMobile',userMobile);
						location.href = constans.htmlUrl+'/html/resetpwd.html';
					}
				}
			})
		}
	})
	
	
})
